<template>
	<view class="height-100 overflow-hidden bg-gray">
		<view class="position-fixed-top" style="z-index: 99;">
			<!-- 分类 -->
			<view class="flex-direction-row justify-around padding-lr padding-tb-sm bg-white">
				<block v-for='(item,index) in typeArray' :key='index'>
					<view class="flex-direction-column align-center" @click="typeChange(index)">
						<text class="text-df" :class="[type===index?'color-df':'color-black-q']">{{item}}</text>
						<view class="bor margin-top-sm" :class="{'bg-df':type===index}"></view>
					</view>
				</block>
			</view>
		</view>
		<view style="height: 100rpx;"></view>
		<!-- 常用处方 -->
		<view v-show="type===0">
			<block v-for='(item,index) in prescriptionDoctorList' :key='index'>
				<view class="bg-white radius-xl padding margin" @click="$navigator('/pages/prescription/common-detail?id='+item.id)">
					<view class="flex-direction-column">
						<text class="text-df color-green">初步诊断</text>
						<text class="text-df margin-top-sm">{{item.primaryDiagnosis}}</text>
						<text class="text-df color-green margin-top">处理意见</text>
						<text class="text-df margin-top-sm">{{item.processAdvice}}</text>
					</view>
					<!-- <view class="flex-direction-row justify-center margin-top col-1">
						<view class="border-df padding-lr-xl margin-tb-sm padding-tb-xsm radius-sl" @click="use(item)">
							<text class="text-smd color-df">使用该处方</text>
						</view>
					</view> -->
				</view>
			</block>
		</view>
		<!-- 历史处方 -->
		<view v-show="type===1">
			<block v-for='(item,index) in doctorPrescriptionList' :key='index'>
				<view class="bg-white radius-xl padding margin" @click="$navigator('/pages/prescription/history-detail?id='+item.id)">
					<view class="flex-direction-row justify-between">
						<view class="">
							<text class="text-df text-bold">患者：</text>
							<text class="text-df color-black-q">{{item.prescriptionCondition?.prescriptionPatient?.name}}</text>
						</view>
						<view class="">
							<text class="text-df text-bold">性别：</text>
							<text class="text-df color-black-q">{{item.prescriptionCondition?.prescriptionPatient?.gender === 0 ? '男' : '女'}}</text>
						</view>
						<view class="">
							<text class="text-df text-bold">年龄：</text>
							<text class="text-df color-black-q">{{item.prescriptionCondition?.prescriptionPatient?.age}}</text>
						</view>
					</view>
					<view class="flex-direction-column margin-tb border-top border-bottom padding-tb">
						<text class="text-df color-green">初步诊断</text>
						<text class="text-df margin-top-sm">{{item.primaryDiagnosis}}</text>
						<text class="text-df color-green margin-top">处理意见</text>
						<text class="text-df margin-top-sm">{{item.processAdvice}}</text>
					</view>
					<view class="">
						<text class="text-df text-bold">处方编号：</text>
						<text class="text-smd color-df">NO.{{item.prescriptionCode}}</text>
					</view>
				</view>
			</block>
		</view>
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom bg-white border-top">
			<view class="padding" @click="addPrescription">
				<view class="bg-df radius-sl padding-tb-sm text-center">
					<text class="text-df color-white">新增常用处方</text>
				</view>
			</view>
			<view style="height: 60rpx;"></view>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad(option) {
			this.prescribingData = option.prescribingData;
			this.userinfo = uni.getStorageSync('userinfo');
			this.getPrescriptionDoctorList();
			this.getDoctorPrescriptionApi();
			// 监听新增了常用处方
			uni.$on('updateSavePrescription',data=>{
				// 回到常用处方的type
				this.type = 0
				// 获取常用处方列表
				this.getPrescriptionDoctorList();
			})
		},
		onUnload() {
			uni.$off('updateSavePrescription')
		},
		data() {
			return {
				typeArray: ['常用处方', '历史处方'],
				type: 0,
				prescriptionDoctorList:[],
				doctorPrescriptionList:[],
				userinfo:{}
			}
		},
		methods: {
			// 常用处方和历史处方的切换
			typeChange(index) {
				if(this.type != index){
					this.type = index;
					// this.pageNum = 1;
					// this.newsList = []
					// this.loading && this.getNews();
				}
			},
			// 请求常用处方列表
			async getPrescriptionDoctorList(){
				const res = await uni.$request.get('/prescriptionDoctorApi/list', {
					'prescriptionPhysician': this.userinfo.id,
					'orderByColumn': 'create_time',
					'isAsc': 'desc',
					// 'reasonable': false,
					// 'pageSize': 10,
					// 'pageNum': this.pageNum
				});
				console.log('常用处方 res: ', res.rows);
				if (res.code === 200) {
					this.prescriptionDoctorList = res.rows;
				} else {
					uni.showModal({
						title: '提示',
						content: '数据查询失败',
						showCancel: false
					});
				}
			},
			// 请求历史处方列表
			async getDoctorPrescriptionApi(){
				const res = await uni.$request.post('/doctorPrescriptionApi/list', {
					prescriptionPhysician: this.userinfo.id
				});
				console.log('历史处方 res: ', res.rows);
				if (res.code === 200) {
					this.doctorPrescriptionList = res.rows;
				} else {
					uni.showModal({
						title: '提示',
						content: '数据查询失败',
						showCancel: false
					});
				}
			},
			// 新增处方
			addPrescription(){
				uni.navigateTo({
					url:"/pages/prescription/addPrescription"
				})
			}
		}
	}
</script>

<style scoped>
	.bor {
		width: 76rpx;
		height: 8rpx;
		/* background: #DB402C; */
		border-radius: 6rpx;
	}
</style>
